package com.hyb.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyb.Util.DateUtil;
import com.hyb.Util.Md5Util;
import com.hyb.Vo.StaffRoleVo;
import com.hyb.entity.Permission;
import com.hyb.entity.Role;
import com.hyb.entity.Staff;
import com.hyb.mapper.PermissionMapper;
import com.hyb.mapper.RoleMapper;
import com.hyb.mapper.StaffMapper;
import com.hyb.page.PageReq;
import com.hyb.page.VoQuery;
import com.hyb.service.StaffService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@Service
public class StaffServiceImpl extends ServiceImpl<StaffMapper, Staff> implements StaffService {

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public Staff getStaffBuyStaffEmail(String staffEmail) {
        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staff_email",staffEmail);
        return staffMapper.selectOne(queryWrapper);
    }

    @Override
    public void updateStaff(Staff staff) {
        String salt = Md5Util.generateUUID();
        String password = Md5Util.md5(staff.getStaffPassword() + salt);
        staff.setStaffSalt(salt);
        staff.setStaffPassword(password);
        staffMapper.updateById(staff);
    }

    @Override
    public List<Map<String, Object>> getMemuListByStaffEmail(String staffEmail) {
        List<String> codes = permissionMapper.selectFatherPermissionCodeByStaffEmail(staffEmail);
        List<Permission> fatherPermissionList = new ArrayList<>();
        for (String s:codes) {
            QueryWrapper<Permission> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("permission_code",s);
            Permission permission = permissionMapper.selectOne(queryWrapper);
            fatherPermissionList.add(permission);
        }

        List<Map<String,Object>> mapList = new ArrayList<>();
        for (Permission permission : fatherPermissionList) {
            Map<String,Object> map = new HashMap<>();
            map.put("menu",permission);
            VoQuery voQuery = new VoQuery();
            voQuery.setStaffEmail(staffEmail);
            voQuery.setFatherId(permission.getPermissionId());
            List<Permission> permissionList = permissionMapper.selectSubPermissionByFather(voQuery);
            map.put("submemu",permissionList);
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public PageReq getAllStaffs(int current, int limit){
        IPage<Staff> page =new Page<>(current,limit);
        QueryWrapper<Staff> wrapper =new QueryWrapper<>();
        IPage<Staff> staffIPage = staffMapper.selectPage(page, wrapper);
        Long total = page.getTotal();
        List<Staff> staffList = staffIPage.getRecords();
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (Staff staff : staffList) {
            Map<String,Object> map = new HashMap<>();
            Set<Role> roles = roleMapper.selectmyRolesByStaffEmail(staff.getStaffEmail());
            map.put("staff",staff);
            map.put("roles",roles);
            mapList.add(map);
        }
        PageReq pageReq = new PageReq();
        pageReq.setTotal(total);
        pageReq.setMyData(mapList);
        return pageReq;
    }

    @Override
    public PageReq getStaffsByWarpper(int current, int limit,String staffName,String role) {
        PageReq pageReq = new PageReq();
        Page<Staff> page = new Page<>(current,limit);
        VoQuery voQuery = new VoQuery();
        if (!StringUtils.isEmpty(staffName)) {
            voQuery.setStaffName(staffName);
        }
        if (!StringUtils.isEmpty(role)) {
            voQuery.setRole(role);
        }

        IPage<Staff> staffsByWarpper = staffMapper.getStaffsByWarpper(page, voQuery);
        List<Staff> staffList = staffsByWarpper.getRecords();
        List<Map<String,Object>> mapList = new ArrayList<>();
        for (Staff staff : staffList) {
            Map<String,Object> map = new HashMap<>();
            Set<String> roles = roleMapper.selectRolesByStaffEmail(staff.getStaffEmail());
            map.put("staff",staff);
            map.put("roles",roles);
            mapList.add(map);
        }
        pageReq.setTotal(page.getTotal());
        pageReq.setMyData(mapList);
        return pageReq;
    }

    @Override
    public void createStaff(StaffRoleVo staffRoleVo) {
        Staff staff  = new Staff();
        staff.setStaffState(staffRoleVo.getStaffState());
        staff.setStaffName(staffRoleVo.getStaffName());
        staff.setStaffBirth(staffRoleVo.getStaffBirth());
        staff.setStaffSex(staffRoleVo.getStaffSex());
        staff.setStaffTel(staffRoleVo.getStaffTel());
        staff.setStaffEmail(staffRoleVo.getStaffEmail());
        String salt = Md5Util.generateUUID();
        staff.setStaffSalt(salt);
        staff.setStaffPassword(Md5Util.md5("123456" + salt));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        staff.setStaffHiredate(LocalDate.parse(DateUtil.getTodayDate(),formatter));
        staffMapper.insert(staff);
    }

    @Override
    public StaffRoleVo getStaffAndRoleBystaffId(int staffId) {
        return staffMapper.getStaffAndRoleBystaffId(staffId);
    }

    @Override
    public void updateStaff(StaffRoleVo staffRoleVo) {
        Staff staff = new Staff();
        staff.setStaffSex(staffRoleVo.getStaffSex());
        staff.setStaffState(staffRoleVo.getStaffState());
        staff.setStaffTel(staffRoleVo.getStaffTel());
        staff.setStaffName(staffRoleVo.getStaffName());
        staff.setStaffId(staffRoleVo.getStaffId());
        staff.setStaffBirth(staffRoleVo.getStaffBirth());
        staff.setStaffEmail(staffRoleVo.getStaffEmail());

        QueryWrapper<Staff> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("staff_id",staffRoleVo.getStaffId());
        staffMapper.update(staff,queryWrapper);
    }

    @Override
    public List<Staff> getAllAccountManager() {
        return staffMapper.selectAllAccountManager();
    }


}
